var express = require('express');
var router = express.Router();

var mongoose = require('mongoose');
var schema = require('../models/dbSchema');

 var jumu = mongoose.model('jumu',schema.jumuSchema);
 var zhedie = mongoose.model('zhedie',schema.selectSchema);
 var fenye = mongoose.model('fenye',schema.teacherSchema);

 var juMu = new Array(),
     guanFang = new Array(),
     sheHui = new Array(),
     mingShi = new Array();

/* GET home page. */
/*---------------------get--------------------*/
router.get('/', function(req, res, next) {

  jumu.find().sort({_id:"-1"}).exec(function(err,data){
     if(err) return console.error(err);
     juMu = data;
  });

  zhedie.find({type:"guanfang"}).limit(5).sort({_id:"-1"}).exec(function(err,data){
      if(err) return console.error(err);
      guanFang = data;
  //  console.log(data[0].title);
  });

  zhedie.find({type:"shehui"}).limit(5).sort({_id:"-1"}).exec(function(err,data){
    if(err) return console.error(err);
    sheHui = data;
  });

  fenye.find().limit(5).sort({_id:"-1"}).exec(function(err,data){
    if(err) return console.error(err);
    mingShi = data;
  });

  res.render('index', { 
    title: '助力公考' ,
    jumu: juMu ,
    guanfang: guanFang,
    shehui: sheHui,
    mingshi: mingShi,
    liuxuefeng: "liuxuefeng2"
  });

});

router.get('/renewcontent', function(req, res, next) {
  res.render('endIndex', { title: '请按说明填写' });
});

/*---------------------post--------------------*/
// 巨幕组
router.post('/renewcontent/jumu',function(req,res,next){

  var is = {
     type    : jumu,
     time    : req.body.time,
     title   : req.body.title,
     content : req.body.content
  };

//  var jumu = mongoose.model('jumu',schema.jumuSchema);
  
  jumu.create(is,function(err){
    if(err) return console.error(err);
  });

console.log(is);

return res.redirect('/renewcontent');

});

// 折叠组
router.post('/renewcontent/zhedie',function(req,res,next){

  var is = {
     type    : req.body.type,
     time    : req.body.time,
     title   : req.body.title,
     content : req.body.content
  };

// var zhedie = mongoose.model('zhedie',schema.selectSchema);
  
  zhedie.create(is,function(err){
    if(err) return console.error(err);
  });

console.log(is);

return res.redirect('/renewcontent');

})

// 分页组
router.post('/renewcontent/fenye',function(req,res,next){

  var is = {
     type    : req.body.type,
     time    : req.body.time,
     title   : req.body.title,
     content : req.body.content
  };

// var fenye = mongoose.model('fenye',schema.teacherSchema);
  
  fenye.create(is,function(err){
    if(err) return console.error(err);
  });

console.log(is);

return res.redirect('/renewcontent');

})


module.exports = router;
